package model;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Screening implements Serializable {
    private String id;
    private Movie movie;
    private Hall hall;
    private LocalDateTime startTime;
    private double price;
    private boolean[][] seats; // true表示已售，false表示空闲

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public Screening(String id, Movie movie, Hall hall, LocalDateTime startTime, double price) {
        this.id = id;
        this.movie = movie;
        this.hall = hall;
        this.startTime = startTime;
        this.price = price;
        this.seats = new boolean[hall.getRows()][hall.getSeatsPerRow()];
    }

    public String getId() {
        return id;
    }

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }

    public Hall getHall() {
        return hall;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public boolean[][] getSeats() {
        return seats;
    }

    public boolean isSeatOccupied(int row, int seat) {
        return seats[row][seat];
    }

    public void setSeat(int row, int seat, boolean occupied) {
        seats[row][seat] = occupied;
    }

    public LocalDateTime getEndTime() {
        return startTime.plusMinutes(movie.getDuration());
    }

    public int getAvailableSeats() {
        int count = 0;
        for (int i = 0; i < hall.getRows(); i++) {
            for (int j = 0; j < hall.getSeatsPerRow(); j++) {
                if (!seats[i][j]) {
                    count++;
                }
            }
        }
        return count;
    }

    public int getSoldSeats() {
        return hall.getTotalSeats() - getAvailableSeats();
    }

    @Override
    public String toString() {
        return "场次ID：" + id + "\n" +
               "电影：" + movie.getTitle() + "\n" +
               "放映厅：" + hall.getName() + "\n" +
               "开始时间：" + startTime.format(DATE_TIME_FORMATTER) + "\n" +
               "票价：" + price + "元\n" +
               "已售座位：" + getSoldSeats() + "\n" +
               "剩余座位：" + getAvailableSeats();
    }
} 